package ui.activity.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import net.ck.hzq.R;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import base.BaseAct;
import base.BaseBiz;
import butterknife.BindView;
import event.NetErrorEvent;
import event.SelectRedballEvent;
import ui.activity.poscontrol.adapter.RedBallCanUseAdapter;
import ui.activity.poscontrol.beanmodel.RedBallBean;
import ui.activity.poscontrol.biz.MyPosControlBiz;


/**
 * 已划拨终端列表
 * Created by chenjian on 18/2/22.
 */

public class UseRedBallAct extends BaseAct{

    MyPosControlBiz biz;



    @BindView(R.id.pushpeople_list)
    RecyclerView pushpeople_list;
    @BindView(R.id.noredballlayout)
    LinearLayout noredballlayout;

    @BindView(R.id.noredballimg)
    ImageView noredballimg;
    private List<RedBallBean.RecordsBean> userInfoData = new ArrayList<>();
    RedBallCanUseAdapter adapter;
    String redballid ="";
    @Override
    protected void setRoot() {
        setContentView(R.layout.activity_redballlist);
    }

    @Override
    protected void setup() {
        biz = new MyPosControlBiz();
        setToolBar("使用红包", "");
        pushpeople_list.setLayoutManager(new LinearLayoutManager(UseRedBallAct.this));
        redballid = getIntent().getStringExtra("redballid");
        if(redballid.equals("2")){
            noredballimg.setBackgroundResource(R.mipmap.redballselected);
        }
        biz.queryCanUseRedball(1, new BaseBiz.Callback<RedBallBean>() {
            @Override
            public void onSuccess(RedBallBean recordsBeans) {
                userInfoData.addAll(recordsBeans.getRecords());
                if(null == adapter){
                    adapter=new RedBallCanUseAdapter(UseRedBallAct.this,userInfoData,redballid);
                    pushpeople_list.setAdapter(adapter);
                }else{
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(RedBallBean recordsBeans) {

            }
        });
        noredballlayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                //把返回数据存入Intent
                intent.putExtra("redballid", "2");//不适用红包
                //设置返回数据
                UseRedBallAct.this.setResult(RESULT_OK, intent);
                //关闭Activity
                UseRedBallAct.this.finish();
            }
        });
    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {


    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void selectRedballEvent(SelectRedballEvent event) {
        Intent intent = new Intent();
        //把返回数据存入Intent
        intent.putExtra("redballid", userInfoData.get(event.toId).getTicketNo());
        intent.putExtra("id", userInfoData.get(event.toId).getId());
        intent.putExtra("redballmoney", userInfoData.get(event.toId).getAmount());
        //设置返回数据
        UseRedBallAct.this.setResult(RESULT_OK, intent);
        //关闭Activity
        UseRedBallAct.this.finish();

    }

}
